package com.hmall.ebiz.controller;

import com.hmall.ebiz.common.ServerResponse;
import com.hmall.ebiz.service.DbConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Thomas Wang
 * @since 2019-02-21
 */
@RestController
@RequestMapping("/dbCode/")
@Api(value = "后台使用的接口", description = "Controller")
public class DbConfigController {

    private DbConfigService dbConfigService;
    @Autowired
    public void setDbConfigService(DbConfigService dbConfigService) {
        this.dbConfigService = dbConfigService;
    }


    @GetMapping("searchConfig")
    @ApiOperation(value = "查找db列表, dbName不传获取所有的")
    public ServerResponse searchConfigModel(@RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageIndex,  String dbName, Integer userId) throws Exception {

        return dbConfigService.searchConfigModel(pageSize, pageIndex, dbName,  userId);
    }


    @GetMapping("deleteById")
    @ApiOperation(value = "删除根据Id")
    public ServerResponse deleteByIdModel(@RequestParam(name = "id") String id, Integer userId) throws Exception {
        return dbConfigService.deleteConfigModel(id, userId);
    }

    @GetMapping("selectById")
    @ApiOperation(value = "查找根据Id")
    public ServerResponse selectByIdModel(@RequestParam(name = "id") Integer id, Integer userId) throws Exception {
        return dbConfigService.selectModelById(id, userId);
    }

    @GetMapping("tableList")
    @ApiOperation(value = "获取Table个数")
    public ServerResponse tableListById(@RequestParam(name = "id") Integer id, Integer userId) throws Exception {
        return dbConfigService.getTableList(id, userId);
    }


    @PostMapping("sendTables")
    @ApiOperation(value = "生成代码链接")
    public ServerResponse createCodeZip(@RequestParam(name = "jsonArray") String tableString, Integer userId, @RequestParam(name = "dbId") Integer dbId) throws Exception {

        return dbConfigService.createCodeZip(tableString, userId, dbId);
    }

    @PostMapping("updateOrAddConfig")
    @ApiOperation(value = "更新或者追加", notes = "type=false追加； json中传：id, dbName, dbDescription, dbType, dbUrl, dbUsername, dbPassword, dbDriverName, author, actvieRecord, secondLevelCache, kotlin, generateList, resultMap, cover, prefix, lomback, builderModel, fieldConstants, parentPackageName, entityPackageName, userId, isPublic")
    public ServerResponse updateOrAddConfig(@RequestParam(name = "jsonModel") String jsonModel, Integer userId, boolean type) throws Exception {

        return dbConfigService.updateOrAddModel(jsonModel, type, userId);
    }

}

